<?php

// $Id: task.inc 24 2010-11-11 07:33:55Z mjohnson60@gmail.com $
//
// $Author: mjohnson60@gmail.com $
// $Date: 2010-10-29 07:27:52 -0400 (Fri, 29 Oct 2010) $
//
// $Rev: 16 $
//


class Task extends DBObject
{
	var $tid = 0;
	
	var $description = 'Please enter a description.';
	var $details = 'Please enter details.';
	var $did = 0;
	var $fid = 0;
	var $mid = 0;
	var $eet = 0;
	var $status = 'P';

	/**
	 * Task::Task()
	 * 
	 * @return
	 */
	public function Task($data = NULL)
	{
		$this->table = 'task';
		
		$this->id_name = 'tid';
		
		$this->fields = array(
			'description' => "'%s'", 'details' => "'%s'", 'did' => '%d', 'fid' => '%d', 'mid' => '%d', 'eet' => '%d', 'status' => "'%s'"
		);
		
		parent::DBObject($data);
	}

	/**
	 * Task::eet_value()
	 * 
	 * @return
	 */
	public function eet_value()
	{
		$days = ($this->eet / 86400.0);
		$hours = (int) (($days - floor($days)) * 24);
		
		return array(
			'days' => (int) $days, 'hours' => $hours
		);
	}

	public static function eet_convert($data = array())
	{
		if (!empty($data))
		{
			$days = ($data['days'] + ($data['hours'] / 24.0)) * 86400;
			return $days;
		}
		
		return FALSE;
	}

	/**
	 * Task::schema()
	 * 
	 * @return
	 */
	public static function schema()
	{
		return 'CREATE TABLE IF NOT EXISTS `task` (
    			  `tid` int(10) unsigned NOT NULL AUTO_INCREMENT,
    			  `description` varchar(255) DEFAULT NULL,
    			  `details` text DEFAULT NULL,
    			  `did` int(10) unsigned NOT NULL,
    			  `fid` int(10) unsigned NOT NULL,
    			  `mid` int(10) unsigned NOT NULL,
    			  `eet` int(10) unsigned NOT NULL,
    			  `status` varchar(1) NOT NULL,
    			  PRIMARY KEY (`tid`),
    			  UNIQUE KEY `tid_UNIQUE` (`tid`)
    			) ENGINE=InnoDB DEFAULT CHARSET=utf8';
	}

	/**
	 * Task::install()
	 * 
	 * @return
	 */
	public static function install()
	{
		db_query('DROP TABLE IF EXISTS task');
		$result = db_query(Task::schema());
		
		if ($result !== FALSE)
		{
			db_query("INSERT INTO task (description, details, did, fid, mid, eet, status) VALUES ('Please enter a description.', 'Please enter details.', 0, 0, 0, 0, 'P')");
			db_query("UPDATE task SET tid=0 WHERE description='Please enter a description.'");
			db_query('ALTER TABLE task AUTO_INCREMENT=1');
			
			system_set_message('The <code>task</code> table was successfully created.');
			
			return TRUE;
		}
		else
		{
			system_set_message('The <code>task</code> table was not created.');
			
			return FALSE;
		}
	}

	/**
	 * Task::uninstall()
	 * 
	 * @return
	 */
	public static function uninstall()
	{
		$result = db_query('DROP TABLE IF EXISTS task');
		if ($result !== FALSE)
		{
			system_set_message('The <code>task</code> table was successfully removed.');
			
			return TRUE;
		}
		else
		{
			system_set_message('The <code>task</code> table was not removed.');
			
			return FALSE;
		}
	}

	public static function status_string($char)
	{
		$output = FALSE;
		switch ($char)
		{
			case 'P' :
				$output = 'Pending';
				break;
			case 'I' :
				$output = 'In Progress';
				break;
			case 'C' :
				$output = 'Completed';
				break;
		}
		
		return $output;
	}

	public static function default_query()
	{
		return 'SELECT t.tid AS tid, t.description AS description, 
						t.details AS details, d.name AS developer, 
						f.description AS feature, m.name AS milestone, 
						t.eet AS eet, t.status AS status
				FROM task t
					INNER JOIN developer d
						ON t.did = d.did
					INNER JOIN feature f
						ON t.fid = f.fid
					INNER JOIN milestone m
						ON t.mid = m.mid
				WHERE t.tid > 0 AND
					d.did > 0 AND
					f.fid > 0 AND
					m.mid > 0';
	}
}
